package she_ji_mo_shi.gong_chang1;

/***
 *@title 工厂方法模式
 *@description <TODO 工厂方法模式>
 *@author 许文迪
 *@version 1.0.0
 *@create 2025/6/24 8:53
 **/
public class GongChangDemo {
    public static void main(String[] args) {
        ShapeFactory shapeFactory = new CircleFactory();
        Shape shape1 = shapeFactory.getShape();
        shape1.draw();

        shapeFactory = new RectangleFactory();
        Shape shape2 = shapeFactory.getShape();
        shape2.draw();
    }
}

// 定义一个接口或抽象类
interface Shape {
    void draw();
}

// 具体实现类
class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}

class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Rectangle::draw() method.");
    }
}

// 抽象工厂类
abstract class ShapeFactory {
    public abstract Shape getShape();
}

// 具体工厂类
class CircleFactory extends ShapeFactory {
    @Override
    public Shape getShape() {
        return new Circle();
    }
}

class RectangleFactory extends ShapeFactory {
    @Override
    public Shape getShape() {
        return new Rectangle();
    }
}
